/*
 * OpenParts
 * A dynamic-loading components framework for GWT
 * Copyright (C) 2011 Christophe Bouthier  [chris{AT}binary-gastronome{DOT}fr]
 *
 * This work is partially based on work I have done at INRIA (http://www.inria.fr) 
 * in the context of the Qualipso European Project (http://qualipso.org/),
 * The work done at INRIA is Copyright (C) 2006-2011 INRIA
 *
 * This work is distributed under the LGPL version 3
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 3
 * as published by the Free Software Foundation. See the GNU
 * Lesser General Public License in LGPL.txt for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package fr.openparts.OpenParts.test.functionnal;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverBackedSelenium;
import org.openqa.selenium.firefox.FirefoxDriver;

import com.thoughtworks.selenium.Selenium;

/**
 * Selenium functional test of Errors Reporting on the OpenParts Showcase
 * 
 * @author Christophe Bouthier [chris{AT}binary-gastronome{DOT}fr]
 * @creation.date 30 March 2011
 */
public class SeleniumTestErrorsReporting {

    @Test
    public void testErrorsReporting() throws InterruptedException {
        WebDriver driver = new FirefoxDriver();

        String baseUrl = "http://127.0.0.1:8080/";
        Selenium selenium = new WebDriverBackedSelenium(driver, baseUrl);

        // load the page and test it's loaded
        selenium.open("/index.html");
        selenium.waitForPageToLoad("30000");
        assertTrue(selenium.isElementPresent("//body[@id='fr-openparts']"));
        assertTrue(selenium.isTextPresent("OpenParts Showcase"));

        // loading the page load the showcase module - test showcase module is loaded
        Thread.sleep(2000);
        assertTrue(selenium.isTextPresent("Errors Reporting"));
        
        // load the errorsreporting module and test it's loaded
        selenium.click("//body[@id='fr-openparts']/div[2]/div[2]/div/div[2]/div/button[4]");
        Thread.sleep(2000);
        assertTrue(selenium.isTextPresent("Errors Reporting Module"));
        
        // loading the errorsreporting module load the errorsdummyservice module - test errorsdummyseervice is loaded
        Thread.sleep(2000);
        assertTrue(selenium.isTextPresent("Generate the error message for no slot attached to proxy"));
        
        // test error message for unknown command
        assertFalse(selenium.isTextPresent("Unknown command: Command type -1 is unknown."));
        selenium.click("//div[@id='OP-errorsreporting-errorsreporting-display1']/div/div[3]/div/div[2]/button");
        assertTrue(selenium.isTextPresent("Unknown command: Command type -1 is unknown."));

        // test error message for load timeout
        assertFalse(selenium.isTextPresent("Load timeout: Loading service unknownService failed (timed out)."));
        selenium.click("//div[@id='OP-errorsreporting-errorsreporting-display1']/div/div[4]/div/div[2]/button");
        Thread.sleep(6000);
        assertTrue(selenium.isTextPresent("Load timeout: Loading service unknownService failed (timed out)."));

        // test error message for no such view (wrong resource)
        assertFalse(selenium.isTextPresent("No such view: Cannot find a view binded for service errorsdummyservice, resource unknownResource and action display."));
        selenium.click("//div[@id='OP-errorsreporting-errorsreporting-display1']/div/div[5]/div/div[2]/button");
        assertTrue(selenium.isTextPresent("No such view: Cannot find a view binded for service errorsdummyservice, resource unknownResource and action display."));

        // test error message for no such view (wrong action)
        assertFalse(selenium.isTextPresent("No such view: Cannot find a view binded for service errorsdummyservice, resource errorsdummyresource and action unknownAction."));
        selenium.click("//div[@id='OP-errorsreporting-errorsreporting-display1']/div/div[6]/div/div[2]/button");
        assertTrue(selenium.isTextPresent("No such view: Cannot find a view binded for service errorsdummyservice, resource errorsdummyresource and action unknownAction."));

        // test error message for no such slot
        assertFalse(selenium.isTextPresent("No such slot: Cannot find a slot with the id unknownSlot in the page."));
        selenium.click("//div[@id='OP-errorsreporting-errorsreporting-display1']/div/div[7]/div/div[2]/button");
        assertTrue(selenium.isTextPresent("No such slot: Cannot find a slot with the id unknownSlot in the page."));

        // test error message for missing mandatory param
        assertFalse(selenium.isTextPresent("Missing mandatory param: Calling view display failed because the mandatory param mandatoryParam was missing."));
        selenium.click("//div[@id='OP-errorsreporting-errorsreporting-display1']/div/div[8]/div/div[2]/button");
        assertTrue(selenium.isTextPresent("Missing mandatory param: Calling view display failed because the mandatory param mandatoryParam was missing."));

        // test error message for no such service
        assertFalse(selenium.isTextPresent("No such service: Cannot find a binded service named unknownService."));
        selenium.click("//div[@id='OP-errorsreporting-errorsreporting-display1']/div/div[9]/div/div[2]/button");
        assertTrue(selenium.isTextPresent("No such service: Cannot find a binded service named unknownService."));
       
        // test error message for failed pre-command when trying to displaying a view
        assertFalse(selenium.isTextPresent("Pre-command failed: The service unknownService1, necessary for the command, wasn't loaded. Trying to load it, before executing the command, failed (timeout)."));
        selenium.click("//div[@id='OP-errorsreporting-errorsreporting-display1']/div/div[10]/div/div[2]/button");
        Thread.sleep(6000);
        assertTrue(selenium.isTextPresent("Pre-command failed: The service unknownService1, necessary for the command, wasn't loaded. Trying to load it, before executing the command, failed (timeout)."));

        // test error message for failed pre-command when trying to dispatch an event to a specific service
        assertFalse(selenium.isTextPresent("Pre-command failed: The service unknownService2, necessary for the command, wasn't loaded. Trying to load it, before executing the command, failed (timeout)."));
        selenium.click("//div[@id='OP-errorsreporting-errorsreporting-display1']/div/div[11]/div/div[2]/button");
        Thread.sleep(6000);
        assertTrue(selenium.isTextPresent("Pre-command failed: The service unknownService2, necessary for the command, wasn't loaded. Trying to load it, before executing the command, failed (timeout)."));

        // test error message for no such slot (from a view proxy)
        assertFalse(selenium.isTextPresent("No such id in the page: Cannot find an element with the id unknownSlot in the page."));
        selenium.click("//div[@id='OP-errorsreporting-errorsreporting-display1']/div/div[12]/div/div[2]/button");
        assertTrue(selenium.isTextPresent("No such id in the page: Cannot find an element with the id unknownSlot in the page."));

        // test error message for given id is not a slot (from a view proxy)
        assertFalse(selenium.isTextPresent("Given id is not a slot: The element with the id dummyId found in the page is not a slot."));
        selenium.click("//div[@id='OP-errorsreporting-errorsreporting-display1']/div/div[13]/div/div[2]/button");
        assertTrue(selenium.isTextPresent("Given id is not a slot: The element with the id dummyId found in the page is not a slot."));

        // test error message for no view attached to this view proxy
        assertFalse(selenium.isTextPresent("No view attached to proxy: There is no view attached to this proxy."));
        selenium.click("//div[@id='OP-errorsreporting-errorsreporting-display1']/div/div[14]/div/div[2]/button");
        assertTrue(selenium.isTextPresent("No view attached to proxy: There is no view attached to this proxy."));

        // test error message for no such slot (from a slot proxy)
        assertFalse(selenium.isTextPresent("No such id in the page: Cannot find an element with the id unknownSlot1 in the page."));
        selenium.click("//div[@id='OP-errorsdummyservice-errorsembeddedresource-display2']/div/div[2]/div/div[2]/button");
        assertTrue(selenium.isTextPresent("No such id in the page: Cannot find an element with the id unknownSlot1 in the page."));

        // test error message for given id is not a slot (from a slot proxy)
        assertFalse(selenium.isTextPresent("Given id is not a slot: The element with the id dummyId1 found in the page is not a slot."));
        selenium.click("//div[@id='OP-errorsdummyservice-errorsembeddedresource-display2']/div/div[3]/div/div[2]/button");
        assertTrue(selenium.isTextPresent("Given id is not a slot: The element with the id dummyId1 found in the page is not a slot."));

        // test error message for no slot attached to slot proxy
        assertFalse(selenium.isTextPresent("No slot attached to proxy: There is no slot attached to this proxy."));
        selenium.click("//div[@id='OP-errorsdummyservice-errorsembeddedresource-display2']/div/div[4]/div/div[2]/button");
        assertTrue(selenium.isTextPresent("No slot attached to proxy: There is no slot attached to this proxy."));
        
        selenium.stop();
    }

}
